﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.field
{
    class PN
    {
        private StringBuilder family_name;
        private StringBuilder given_name;
        private StringBuilder middle_name;
        private StringBuilder suffix;
        private StringBuilder prefix;
        private StringBuilder degree;

        public StringBuilder Family_name { get { return family_name; } set { family_name = value; } }
        public StringBuilder Given_name { get { return given_name; } set { given_name = value; } }
        public StringBuilder Middle_name { get { return middle_name; } set { middle_name = value; } }
        public StringBuilder Suffix { get { return suffix; } set { suffix = value; } }
        public StringBuilder Prefix { get { return prefix; } set { prefix = value; } }
        public StringBuilder Degree { get { return degree; } set { degree = value; } }
    
        public PN()
        {
            family_name = new StringBuilder();
            given_name = new StringBuilder();
            middle_name = new StringBuilder();
            suffix=new StringBuilder();
            prefix=new StringBuilder();
            degree=new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }
        public int fill(string source,int temp)
        {
            int i, x = 0;

            for (i = temp; i < source.Length; i++)
            {
               if(check(source,i)!=null) 
               {
                   family_name.Capacity = family_name.Length;
                   Given_name.Capacity = given_name.Length;
                   middle_name.Capacity = given_name.Length;
                   suffix.Capacity = suffix.Length;
                   prefix.Capacity = prefix.Length;
                   degree.Capacity = degree.Length;
                   return i-1;         
               }
                else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    family_name.Capacity = family_name.Length;
                    Given_name.Capacity = given_name.Length;
                    middle_name.Capacity = given_name.Length;
                    suffix.Capacity = suffix.Length;
                    prefix.Capacity = prefix.Length;
                    degree.Capacity = degree.Length;
                    return i;                    
                }
                else if (x == 0)
                    family_name.Insert(family_name.Length, source[i]);
                else if (x == 1)
                    given_name.Insert(given_name.Length, source[i]);
                else if (x == 2)
                    middle_name.Insert(given_name.Length, source[i]);
                else if (x == 3)
                    suffix.Insert(suffix.Length, source[i]);
                else if (x == 4)
                    prefix.Insert(prefix.Length, source[i]);
                else if (x == 5)
                    degree.Insert(degree.Length, source[i]);
            }
            return i;
        }

        internal int fill(char p, int i)
        {
            throw new NotImplementedException();
        }

        public string tostring()
        {
            string PN_string = "";

            if (family_name.Length != 0)
                PN_string += family_name;
            if (given_name.Length != 0)
                PN_string += "^" + given_name;
            if (middle_name.Length != 0)
                PN_string += "^" + middle_name;
            if (suffix.Length != 0)
                PN_string += "^" + suffix;
            if (prefix.Length != 0)
                PN_string += "^" + prefix;
            if (degree.Length != 0)
                PN_string += "^" + degree;
            return PN_string;
        }
    }
}
